package org.example.demo3.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import org.example.demo3.dto.MessageDto;
import org.example.demo3.enity.User;
import org.example.demo3.enity.message;
import org.example.demo3.mapper.MessageMapper;
import org.example.demo3.mapper.UserMapper;
import org.example.demo3.service.MessageService;
import org.example.demo3.untils.AliOSSUtils;
import org.example.demo3.untils.BeanCopyUtils;
import org.example.demo3.vo.messageVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@Service
public class MessageServiceImpl extends ServiceImpl<MessageMapper, message> implements MessageService {
   @Autowired
    private MessageMapper messageMapper;

    @Autowired
    private AliOSSUtils aliOSSUtils;

    @Autowired
    private UserMapper userMapper;




    @Override
    public List<messageVo> listmessage(MessageDto messagedto) {
        QueryWrapper<message> wrapper = new QueryWrapper<>();
        wrapper.eq("pid", messagedto.getPid());
        List<message> messages = messageMapper.selectList(wrapper);
        List<messageVo> messageVos = new ArrayList<>();
        for (message msg : messages) {
            messageVo msgVo = new messageVo();
            BeanUtils.copyProperties(msg,msgVo);
            messageVos.add(msgVo);
        }

        
        return messageVos;
    }

    @Override
    public void addMessage(MessageDto message1, MultipartFile[] files) throws IOException {
        message newMessage =new message();
        BeanUtils.copyProperties(message1, newMessage);
        System.out.println(newMessage);
        User user = userMapper.selectById(newMessage.getUid());
        for (MultipartFile file : files) {
          String url = aliOSSUtils.upload(file);
          newMessage.setPhoto(url);
        }
        newMessage.setName(user.getNickname());
        newMessage.setPhotouser(user.getAvatar());

        messageMapper.insert(newMessage);
    }

    @Override
    public boolean delete(MessageDto messagedto){
        QueryWrapper<message> wrapper = new QueryWrapper<>();
        wrapper.eq("pid", messagedto.getPid());
        int messages = messageMapper.delete(wrapper);
        return messages > 0;
    }






}
